//
//  ViewController.m
//  WrittingAnimation
//
//  Created by mac on 2019/4/27.
//  Copyright © 2019 Yang. All rights reserved.
//

#import "ViewController.h"
#import "TextRenderingTools.h"
#import "ArticleModel.h"
#import "CheckiPhoneTool.h"

#import "TextAnimationTool.h"

@interface ViewController ()

@property (nonatomic, strong) UIScrollView *scrollView;

@property (nonatomic, strong) NSArray *textArr;
@property (nonatomic, strong) NSString *text;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    //    self.scrollView.autoresizesSubviews = NO;
    
    [self.view addSubview:self.scrollView];
    
//    self.scrollView.backgroundColor = [UIColor grayColor];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"文字绘制" style:UIBarButtonItemStylePlain target:self action:@selector(textDrawAnimation)];
    
    
    NSString *articleName = _model.ArticleName;
    NSString *articleAuthor = _model.ArticleAuthor;
    NSString *articleContent = _model.ArticleContent;
    NSArray *articleArr = [articleContent componentsSeparatedByString:@"\n"];
    NSMutableArray *contentArr = [NSMutableArray new];
    NSLog(@"articleArr = %@",articleArr);
    for (NSString *object in articleArr) {
        //        NSString *text = [object stringByReplacingOccurrencesOfString:@" " withString:@""];
        //去掉字符串两端的空格及回车
        NSString *temp = [object stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
        NSString *text = [temp stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
        NSLog(@"text = %@",text);
        [contentArr addObject:text];
    }
    NSMutableArray *textArr = [NSMutableArray new];
    [textArr addObject:articleName];
    [textArr addObject:articleAuthor];
    [textArr addObjectsFromArray:contentArr];
    
    _textArr = [NSArray arrayWithArray:textArr];
    //    [[TextAnimationTool shareInstance] showTextArr:_textArr andFontBgColor:[UIColor clearColor] andFontColor:[UIColor cyanColor] andFillColor:[UIColor yellowColor] andFontSize:45 superView:_scrollView];
    if (_isPortrait) {
//
//        TextAnimationTool *tool = [[TextAnimationTool alloc] init];
//        NSLog(@"tool = %@",tool);
        
//        [[TextAnimationTool shareInstance] drawAnimationWithTextArr:_textArr withType:TextAnimationTypePortrait fontColor:[UIColor redColor] font:[UIFont systemFontOfSize:45] superView:self.scrollView];
//        NSLog(@"[TextAnimationTool shareInstance] = %@",[TextAnimationTool shareInstance]);
        
        [[TextAnimationTool shareInstance] showWithTextArr:_textArr withType:TextAnimationTypePortrait fontColor:[UIColor redColor] font:[UIFont systemFontOfSize:45] superView:self.scrollView];
    }
    else
    {
        TextRenderingTools *tools = [[TextRenderingTools alloc] init];
        [tools showTextArr:_textArr andFontBgColor:[UIColor clearColor] andFontColor:[UIColor redColor] andFillColor:[UIColor clearColor] andFontSize:45 superView:self.scrollView];
    }
    
    //
    //    [tools drawAnimationWithText:@"你好啊，我叫杨洋" andFontBgColor:[UIColor clearColor] andFontColor:[UIColor redColor] andFillColor:[UIColor clearColor] andFontSize:45 superView:scrollView];
    //
    //
    ////    [tools showWithText:@"你好啊，我叫杨洋" andFontBgColor:[UIColor clearColor] andFontColor:[UIColor redColor] andFillColor:[UIColor clearColor] andFontSize:45 superView:scrollView];
    //
    ////    [tools drawAnimationWithTextArr:textArr andFontBgColor:[UIColor clearColor] andFontColor:[UIColor redColor] andFillColor:[UIColor clearColor] andFontSize:45 superView:scrollView];
    //
    //    [tools setDrawAnimationFinishedBlock:^{
    //        NSLog(@"在控制器中结束");
    //    }];
    
    
    //    UIView *textView = [[UIView alloc] init];
    //    textView.backgroundColor = [UIColor grayColor];
    //    [self.view addSubview:textView];
    //
    //    [textView mas_makeConstraints:^(MASConstraintMaker *make) {
    //        make.left.mas_equalTo(self.view.mas_left).mas_offset(20);
    //        make.right.mas_equalTo(self.view.mas_right).mas_offset(-20);
    //        make.centerY.mas_equalTo(self.view.mas_centerY).mas_offset(0);
    //        make.height.mas_equalTo(300);
    //    }];
    //
    //    TextRenderingTools *tools = [[TextRenderingTools alloc] init];
    //
    ////    tools.superViewWidth = kScreenWidth - 40;
    ////    tools.superViewHeight = 300;
    //
    //    tools.superViewSize = CGSizeMake(kScreenWidth - 40, 300);
    
    //        [tools drawAnimationWithTextArr:@[@"我叫杨洋",@"我喜欢编程",@"你喜欢编程吗？我喜欢编程，更喜欢你！"] andFontBgColor:[UIColor clearColor] andFontColor:[UIColor purpleColor] andFillColor:[UIColor clearColor] andFontSize:40 superView:textView];
    //
    //        [tools drawAnimationWithText:@"你喜欢编程吗？我喜欢编程，更喜欢你！" andFontBgColor:[UIColor clearColor] andFontColor:[UIColor purpleColor] andFillColor:[UIColor clearColor] andFontSize:40 superView:textView];
    //
    //        [tools showTextArr:@[@"我叫杨洋",@"我喜欢编程",@"你喜欢编程吗？我喜欢编程，更喜欢你！"] andFontBgColor:[UIColor clearColor] andFontColor:[UIColor purpleColor] andFillColor:[UIColor clearColor] andFontSize:40 superView:textView];
    //
    //
    //    [tools showWithText:@"你喜欢编程吗？我喜欢编程，更喜欢你！" andFontBgColor:[UIColor clearColor] andFontColor:[UIColor purpleColor] andFillColor:[UIColor clearColor] andFontSize:40 superView:textView];
    
    
}

-(void)textDrawAnimation
{
    
    
    
    
    
    
    
    [_scrollView removeFromSuperview];
    _scrollView = nil;
    
    self.navigationItem.rightBarButtonItem = nil;
    
    [self.view addSubview:self.scrollView];
    
    //    [[TextAnimationTool shareInstance] drawAnimationWithTextArr:_textArr andFontBgColor:[UIColor clearColor] andFontColor:[UIColor cyanColor] andFontSize:45 superView:_scrollView];
    //    __weak typeof(self) weakSelf = self;
    //    [[TextAnimationTool shareInstance] setDrawAnimationFinishedBlock:^{
    //        weakSelf.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"文字绘制" style:UIBarButtonItemStylePlain target:weakSelf action:@selector(textDrawAnimation)];
    //    }];
    
    if (_isPortrait) {
        [[TextAnimationTool shareInstance] drawAnimationWithTextArr:_textArr withType:TextAnimationTypePortrait fontColor:[UIColor redColor] font:[UIFont systemFontOfSize:45] superView:self.scrollView];
    }
    else
    {
        TextRenderingTools *tools = [[TextRenderingTools alloc] init];
        [tools drawAnimationWithTextArr:_textArr andFontBgColor:[UIColor clearColor] andFontColor:[UIColor redColor] andFillColor:[UIColor clearColor] andFontSize:45 superView:self.scrollView];
        __weak typeof(self) weakSelf = self;
        [tools setDrawAnimationFinishedBlock:^{
            weakSelf.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"文字绘制" style:UIBarButtonItemStylePlain target:weakSelf action:@selector(textDrawAnimation)];
        }];
    }
    
    
    
}

-(UIScrollView *)scrollView
{
    if (!_scrollView) {
        CGFloat navigationBarHeight = 64;
        if ([CheckiPhoneTool isiPhoneX]) {
            navigationBarHeight = 88;
        }
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, navigationBarHeight, kScreenWidth, kScreenHeight - navigationBarHeight)];
    }
    return _scrollView;
}

-(void)dealloc
{
    NSLog(@"销毁文字动画控制器");
}

@end























////
////  ViewController.m
////  WrittingAnimation
////
////  Created by mac on 2019/4/27.
////  Copyright © 2019 Yang. All rights reserved.
////
//
//#import "ViewController.h"
//#import "TextRenderingTools.h"
//#import "ArticleModel.h"
//#import "CheckiPhoneTool.h"
//
//#import "TextAnimationTool.h"
//
//@interface ViewController ()
//
//@property (nonatomic, strong) UIScrollView *scrollView;
//
//@property (nonatomic, strong) NSArray *textArr;
//@property (nonatomic, strong) NSString *text;
//
//@end
//
//@implementation ViewController
//
//- (void)viewDidLoad {
//    [super viewDidLoad];
//    self.view.backgroundColor = [UIColor whiteColor];
//
//    self.automaticallyAdjustsScrollViewInsets = NO;
////    self.scrollView.autoresizesSubviews = NO;
//
//    [self.view addSubview:self.scrollView];
//
//    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"文字绘制" style:UIBarButtonItemStylePlain target:self action:@selector(textDrawAnimation)];
//
//
//    NSString *articleName = _model.ArticleName;
//    NSString *articleAuthor = _model.ArticleAuthor;
//    NSString *articleContent = _model.ArticleContent;
//    NSArray *articleArr = [articleContent componentsSeparatedByString:@"\n"];
//    NSMutableArray *contentArr = [NSMutableArray new];
//    NSLog(@"articleArr = %@",articleArr);
//    for (NSString *object in articleArr) {
////        NSString *text = [object stringByReplacingOccurrencesOfString:@" " withString:@""];
//        //去掉字符串两端的空格及回车
//        NSString *temp = [object stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
//        NSString *text = [temp stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
//        NSLog(@"text = %@",text);
//        [contentArr addObject:text];
//    }
//    NSMutableArray *textArr = [NSMutableArray new];
//    [textArr addObject:articleName];
//    [textArr addObject:articleAuthor];
//    [textArr addObjectsFromArray:contentArr];
//
//    _textArr = [NSArray arrayWithArray:textArr];
////    [[TextAnimationTool shareInstance] showTextArr:_textArr andFontBgColor:[UIColor clearColor] andFontColor:[UIColor cyanColor] andFillColor:[UIColor yellowColor] andFontSize:45 superView:_scrollView];
//
//    TextRenderingTools *tools = [[TextRenderingTools alloc] init];
//    [tools showTextArr:_textArr andFontBgColor:[UIColor clearColor] andFontColor:[UIColor redColor] andFillColor:[UIColor clearColor] andFontSize:45 superView:self.scrollView];
////
////    [tools drawAnimationWithText:@"你好啊，我叫杨洋" andFontBgColor:[UIColor clearColor] andFontColor:[UIColor redColor] andFillColor:[UIColor clearColor] andFontSize:45 superView:scrollView];
////
////
//////    [tools showWithText:@"你好啊，我叫杨洋" andFontBgColor:[UIColor clearColor] andFontColor:[UIColor redColor] andFillColor:[UIColor clearColor] andFontSize:45 superView:scrollView];
////
//////    [tools drawAnimationWithTextArr:textArr andFontBgColor:[UIColor clearColor] andFontColor:[UIColor redColor] andFillColor:[UIColor clearColor] andFontSize:45 superView:scrollView];
////
////    [tools setDrawAnimationFinishedBlock:^{
////        NSLog(@"在控制器中结束");
////    }];
//
//
////    UIView *textView = [[UIView alloc] init];
////    textView.backgroundColor = [UIColor grayColor];
////    [self.view addSubview:textView];
////
////    [textView mas_makeConstraints:^(MASConstraintMaker *make) {
////        make.left.mas_equalTo(self.view.mas_left).mas_offset(20);
////        make.right.mas_equalTo(self.view.mas_right).mas_offset(-20);
////        make.centerY.mas_equalTo(self.view.mas_centerY).mas_offset(0);
////        make.height.mas_equalTo(300);
////    }];
////
////    TextRenderingTools *tools = [[TextRenderingTools alloc] init];
////
//////    tools.superViewWidth = kScreenWidth - 40;
//////    tools.superViewHeight = 300;
////
////    tools.superViewSize = CGSizeMake(kScreenWidth - 40, 300);
//
////        [tools drawAnimationWithTextArr:@[@"我叫杨洋",@"我喜欢编程",@"你喜欢编程吗？我喜欢编程，更喜欢你！"] andFontBgColor:[UIColor clearColor] andFontColor:[UIColor purpleColor] andFillColor:[UIColor clearColor] andFontSize:40 superView:textView];
////
////        [tools drawAnimationWithText:@"你喜欢编程吗？我喜欢编程，更喜欢你！" andFontBgColor:[UIColor clearColor] andFontColor:[UIColor purpleColor] andFillColor:[UIColor clearColor] andFontSize:40 superView:textView];
////
////        [tools showTextArr:@[@"我叫杨洋",@"我喜欢编程",@"你喜欢编程吗？我喜欢编程，更喜欢你！"] andFontBgColor:[UIColor clearColor] andFontColor:[UIColor purpleColor] andFillColor:[UIColor clearColor] andFontSize:40 superView:textView];
////
////
////    [tools showWithText:@"你喜欢编程吗？我喜欢编程，更喜欢你！" andFontBgColor:[UIColor clearColor] andFontColor:[UIColor purpleColor] andFillColor:[UIColor clearColor] andFontSize:40 superView:textView];
//
//
//}
//
//-(void)textDrawAnimation
//{
//
//
//
//
//
//
//
//    [_scrollView removeFromSuperview];
//    _scrollView = nil;
//
//    self.navigationItem.rightBarButtonItem = nil;
//
//    [self.view addSubview:self.scrollView];
//
////    [[TextAnimationTool shareInstance] drawAnimationWithTextArr:_textArr andFontBgColor:[UIColor clearColor] andFontColor:[UIColor cyanColor] andFontSize:45 superView:_scrollView];
////    __weak typeof(self) weakSelf = self;
////    [[TextAnimationTool shareInstance] setDrawAnimationFinishedBlock:^{
////        weakSelf.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"文字绘制" style:UIBarButtonItemStylePlain target:weakSelf action:@selector(textDrawAnimation)];
////    }];
//
//    TextRenderingTools *tools = [[TextRenderingTools alloc] init];
//    [tools drawAnimationWithTextArr:_textArr andFontBgColor:[UIColor clearColor] andFontColor:[UIColor redColor] andFillColor:[UIColor clearColor] andFontSize:45 superView:self.scrollView];
//    __weak typeof(self) weakSelf = self;
//    [tools setDrawAnimationFinishedBlock:^{
//        weakSelf.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"文字绘制" style:UIBarButtonItemStylePlain target:weakSelf action:@selector(textDrawAnimation)];
//    }];
//
//}
//
//-(UIScrollView *)scrollView
//{
//    if (!_scrollView) {
//        CGFloat navigationBarHeight = 64;
//        if ([CheckiPhoneTool isiPhoneX]) {
//            navigationBarHeight = 88;
//        }
//        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, navigationBarHeight, kScreenWidth, kScreenHeight - navigationBarHeight)];
//    }
//    return _scrollView;
//}
//
//-(void)dealloc
//{
//    NSLog(@"销毁文字动画控制器");
//}
//
//@end
